import {login, logout, profile, refresh} from '@/api/auth'
import storage from '@/utils/storage' // sessionStorage
import {resetRouter} from '@/router'
import store from '@/store'
import astrict from '@/utils/astrict'

const getDefaultState = () => {
  return {
    token: storage.getItem('token'),
    name: '',
    userInfo: '',
    companyName: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERINFO: (state, info) => {
    state.userInfo = info
  },
  SET_COMPANYNAME: (state, value) => {
    state.companyName = value
  }
}

const actions = {
  // 用户登录
  login({commit}, userInfo) {
    const {loginName, password} = userInfo
    return new Promise((resolve, reject) => {
      login({
        loginName: loginName.trim(),
        password: password
      }).then(res => {
        // 保存token到store
        commit('SET_TOKEN', res.result)
        // 本地存储方法
        storage.setItem('token', res.result)
        // 保存登录获取token的时间
        storage.setItem('tokenLastTime', new Date().getTime())

        storage.setItem('isLogin', true)
        astrict()  // 开启定时器
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户信息
  profile({commit, state}) {
    return new Promise(async (resolve, reject) => {

      let {result: data} = await profile().catch((err) => {
        return reject(err)
      })

      /*
        @userName 用户名
        @mobile 用户手机
        @id 用户id
        @sex 性别
      */
      const {userName, id, mobile, sex} = data

      // 保存用户名到store
      commit('SET_NAME', userName)
      // 用户信息对象
      const userInfo = {
        userName,
        id,
        mobile,
        sex,
      }
      // 保存用户信息到store
      commit('SET_USERINFO', userInfo)
      // 保存公司名称到store
      commit('SET_COMPANYNAME', 'Mall-UI')
      resolve(data)
    })
  },

  // 用户登出
  logout({commit, state}) {
    return new Promise((resolve, reject) => {
      logout().then(() => {
        // 清除storage
        storage.clear()
        // 充重置路由
        resetRouter()
        // 清除状态
        commit('RESET_STATE')
        // 清除状态
        store.commit('permission/RESET_STATE')
        store.commit('tagsView/REMOVE_VIEWS')
        resolve()
      }).catch(error => {
        reject(error)
      })

    })
  },

  // 刷新token
  refreshToken({commit}) {
    return new Promise((resolve, reject) => {
      refresh().then(res => {
        // 保存token到store
        commit('SET_TOKEN', res.result)
        // 本地存储方法
        storage.setItem('token', res.result)
        // 保存获取刷新token的时间
        storage.setItem('tokenLastTime', new Date().getTime())
        resolve()
      }).catch(error => reject(error))
    })
  },

  // 重置token
  resetToken({commit}) {
    return new Promise(resolve => {
      storage.clear() // must remove  token  first
      commit('RESET_STATE')
      // 清除状态
      store.commit('permission/RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
